//
//  MCHeaderView.m
//  MCModify
//
//  Created by 陆绍波 on 2018/11/15.
//  Copyright © 2018 陆绍波. All rights reserved.
//

#import "MCHeaderView.h"
#import "MCHeaderModel.h"
#import "MCHeaderCell.h"
#import <TZImagePickerController.h>
@interface MCHeaderView()<UICollectionViewDelegate,UICollectionViewDataSource,TZImagePickerControllerDelegate>
@property(nonatomic,strong) UICollectionView *collectionView;
@property(nonatomic,strong) NSMutableArray <MCHeaderModel *> *dataSource;
@end

static NSString *const CellId = @"MCHeaderCell";
@implementation MCHeaderView

//- (IBAction)headButton:(id)sender {
//    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:9 delegate:self];
//    [[self viewController] presentViewController:imagePickerVc animated:YES completion:nil];
//}

- (UIViewController *)viewController {
    for (UIView *view = self; view; view = view.superview) {
        UIResponder *nextResponder = [view nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        _dataSource = [[NSMutableArray alloc]init];
        for (int i=0; i<8; i++) {
            MCHeaderModel *model = [[MCHeaderModel alloc]init];
            model.image = [UIImage imageNamed:@"personal_headSquare"];
            [_dataSource addObject:model];
        }
        [self setupCollectionView];
    }
    return self;
}

- (void)setupCollectionView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumLineSpacing = 8;
    layout.minimumInteritemSpacing = 8;
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 200, UIScreen.mainScreen.bounds.size.width, 200) collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor clearColor];
    [self addSubview:self.collectionView];
    

    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [self.collectionView registerClass:[MCHeaderCell class] forCellWithReuseIdentifier:CellId];
    [self.collectionView reloadData];
}

#pragma mark - collectionView 的数据源方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MCHeaderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellId forIndexPath:indexPath];
    cell.model = _dataSource[indexPath.item];
    return cell;
    
}

#pragma mark - collectionView的代理方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:nil];
    
    __weak typeof(self) weak_self = self;
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        __strong typeof(weak_self) strong_self = weak_self;
        if (strong_self) {
            UIImage *image = photos.firstObject;
            strong_self->_dataSource[indexPath.item].image = image;
            [collectionView reloadData];
            strong_self->_dataSource[indexPath.item+1].image = [UIImage imageNamed:@"AlbumAddBtn"];
        }
    }];
    
    //        UIViewController *controller = [self.viewController];
    [[self viewController] presentViewController:imagePickerVc animated:YES completion:nil];
}

#pragma mark - 流式布局
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(([UIScreen mainScreen].bounds.size.width -32) / 4, ([UIScreen mainScreen].bounds.size.width -32) / 4);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}


@end
